package com.ltu.util.ext.wx.wxPay;

import org.springframework.util.StringUtils;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.ltu.util.IpUtil;

import javax.servlet.http.HttpServletRequest;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.StringTokenizer;

public class ServletUtil {

    public static void showHeaders(HttpServletRequest httpServletRequest) {
        Enumeration e = httpServletRequest.getHeaderNames();
        while (e.hasMoreElements()) {
            String key = (String) e.nextElement();
            String val = httpServletRequest.getHeader(key);
            System.out.println(key + ":" + val);
        }
    }

    /**
     * 获取指定请求路径带的参数的指定值
     * @param path
     * @param paramName
     * @return
     */
    public static String getParamValueFromRequestPathByStrToken(String path,String paramName){
        if (StringUtils.isEmpty(path)&&StringUtils.isEmpty(paramName))
            return null;
        String sub=path.substring(path.indexOf("?")+1,path.length());
        if (StringUtils.isEmpty(sub))
            return null;
        StringTokenizer stringTokenizer=new StringTokenizer(sub,"&=");
        while (stringTokenizer.hasMoreTokens()){
            if (stringTokenizer.nextToken().equals(paramName))
                return stringTokenizer.nextToken();
        }
        return null;
    }

    /**
     * 获取参数键值对
     *
     * @param request
     * @return
     */
    public static Map<String, Object> getParamsFromRequest(HttpServletRequest request) {
        Enumeration e = request.getParameterNames();
        Map<String, Object> result = new HashMap<String, Object>();
        while (e.hasMoreElements()) {
            String key = (String) e.nextElement();
            Object val = request.getParameter(key);
            result.put(key, val);
        }
        return result;
    }

    /**
     * 生成url参数后缀
     *
     * @param args
     * @return
     */
    public static String generateUrlFix(Object... args) {
        String result = "";
        for (int i = 0; i < args.length; i += 2) {
            String key = (String) args[i];
            Object val = args[i + 1];
            if (val == null)
                continue;
            result += "&" + key + "=" + val;
        }
        return result.equals("") ? null : result;
    }

    /**
     * 生成url参数后缀
     *
     * @param params
     * @return
     */
    public static String generateUrlFixByMap(String url, Map<String, Object> params) {
        String result = "";
        for (Map.Entry<String, Object> entry : params.entrySet()) {
            String key = entry.getKey();
            Object val = entry.getValue();
            if (val == null)
                continue;
            result += "&" + key + "=" + val;
        }
        return result.equals("") ? null : url+"?"+result.substring(1);
    }


    /**
     * 获取当前线程的httpservlet
     *
     * @return
     */
    public static HttpServletRequest getCurrentServletRequest() {
        return ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
    }
    
    /**
     * 获取当前请求的IP
     *
     * @return
     */
    public static String getCurrentRequestIp() {
    	HttpServletRequest req = getCurrentServletRequest();
    	return  IpUtil.getIpAddr(req);
    }
    

    /**
     * 获取当前线程的sessionId
     *
     * @return
     */
    public static String getCurrentSessionId() {
        return getCurrentServletRequest().getSession().getId();
    }

    /**
     * 获取当前用户IP(注：在使用反向代理时候getRemoteAddr不生效，要使用nginx带过来的远程ip头部信息获取)
     *
     * @return
     */
    public static String getHost() {
        String host=getCurrentServletRequest().getHeader("x-real-ip");//nginx配置独有
        if (host!=null)
            return host;
        return ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest().getRemoteAddr();
    }

    public static boolean isAjaxRequest(HttpServletRequest httpRequest) {
        if (org.apache.commons.lang.StringUtils.equalsIgnoreCase("XMLHttpRequest", httpRequest.getHeader("X-Requested-With"))) {
            return true;
        }
        return false;
    }
}
